package action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import model.Choice;
import model.Fill;
import model.Judge;
import model.PickedChoice;
import model.PickedJudge;
import model.PickedFill;
import model.Teacher;
import model.Test;


import com.opensymphony.xwork2.ActionSupport;

import dao.BasicDao;
import dao.ResultDAO;
import dao.TeacherDAO;
import dao.TestDAO;

@SuppressWarnings("serial")
public class TeacherAction extends ActionSupport {
	private Teacher teacher = new Teacher();
	private BasicDao basicDao = new BasicDao();
	// 登录
	private String username;
	private String password;
	// 组卷
	private int CValue;
	private int FValue;
	private int JValue;
	private int CNum;
	private int FNum;
	private int JNum;
	private String name;
	private String date;
	private int time;
	private Test test = new Test();
	//查看结果
	int testId;
	private List<Object> tests=null;
	private List<String> dates=new ArrayList<String>();

	public int getTestId() {
		return testId;
	}

	public void setTestId(int testId) {
		this.testId = testId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public BasicDao getBasicDao() {
		return basicDao;
	}

	public void setBasicDao(BasicDao basicDao) {
		this.basicDao = basicDao;
	}

	public int getCValue() {
		return CValue;
	}

	public void setCValue(int cValue) {
		CValue = cValue;
	}

	public int getFValue() {
		return FValue;
	}

	public void setFValue(int fValue) {
		FValue = fValue;
	}

	public int getJValue() {
		return JValue;
	}

	public void setJValue(int jValue) {
		JValue = jValue;
	}

	public int getCNum() {
		return CNum;
	}

	public void setCNum(int cNum) {
		CNum = cNum;
	}

	public int getFNum() {
		return FNum;
	}

	public void setFNum(int fNum) {
		FNum = fNum;
	}

	public int getJNum() {
		return JNum;
	}

	public void setJNum(int jNum) {
		JNum = jNum;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setTest(Test test) {
		this.test = test;
	}

	public Test getTest() {
		return test;
	}

	public String login() {
		List<Object> teacherList=basicDao.getObjectByName("Teacher", username);
		if (teacherList!=null&&teacherList.size()!=0) {
			teacher = (Teacher)teacherList.get(0);
			if (teacher.getPwd().equals(password)) {
				return SUCCESS;
			}else {
				return "failure";
			}
		} else {
			return "failure";
		}
	}

	public String formpaper() {
		//转换格式，先从string到date，再从date到string，再从string到timestamp
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date temp1 = null;
		if (date != null && !"".equals(date)) {
			try {
				temp1 = format.parse(date);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
		String temp2 = format.format(temp1);
		Timestamp timestamp = Timestamp.valueOf(temp2);
		
		test.setDate(timestamp);
		test.setCNum(CNum);
		test.setCValue(CValue);
		test.setFNum(FNum);
		test.setFValue(FValue);
		test.setJNum(JNum);
		test.setJValue(JValue);
		test.setName(name);
		test.setTime(time);
		basicDao.save(test);
		if (pickquestion(test.getId())) {
			return SUCCESS;
		} else {
			return "failure";
		}
	}

	public Boolean pickquestion(int id) {
		PickedChoice pickedchoice=new PickedChoice();
		List<Object> choosequestions=basicDao.getAllObjects("Choice");
		int amount=choosequestions.size();
		int n=CNum;
		if (n>amount) {
			return false;
		}
		int[] t=new int[n];
		for (int j = 0; j < t.length; j++) {
			int temp=(int) (Math.random()*amount);
			Boolean flag=true;
			for (int k = 0; k < t.length; k++) {
				if (t[k]==temp) {
					flag=false;
				}
			}
			if (flag) {
				t[j]=temp;
			} else {
				j--;
			}
		}
		for (int j = 0; j < t.length; j++) {
			Choice choice=(Choice)choosequestions.get(t[j]);
			pickedchoice=new PickedChoice();
			pickedchoice.setChoiceId(choice.getId());
			pickedchoice.setTestId(id);
			basicDao.save(pickedchoice);
		}
		
		PickedFill pickedfill=new PickedFill();
		List<Object> fillquestions=basicDao.getAllObjects("Fill");
		amount=fillquestions.size();
		n=FNum;
		if (n>amount) {
			return false;
		}
		t=new int[n];
		for (int j = 0; j < t.length; j++) {
			int temp=(int) (Math.random()*amount);
			Boolean flag=true;
			for (int k = 0; k < t.length; k++) {
				if (t[k]==temp) {
					flag=false;
				}
			}
			if (flag) {
				t[j]=temp;
			} else {
				j--;
			}
		}
		for (int j = 0; j < t.length; j++) {
			Fill fill=(Fill)fillquestions.get(t[j]);
			pickedfill=new PickedFill();
			pickedfill.setFillId(fill.getId());
			pickedfill.setTestId(id);
			basicDao.save(pickedfill);
		}
		
		PickedJudge pickedjudge=new PickedJudge();
		List<Object> judgequestions=basicDao.getAllObjects("Judge");
		amount=judgequestions.size();
		n=JNum;
		if (n>amount) {
			return false;
		}
		t=new int[n];
		for (int j = 0; j < t.length; j++) {
			int temp=(int) (Math.random()*amount);
			Boolean flag=true;
			for (int k = 0; k < t.length; k++) {
				if (t[k]==temp) {
					flag=false;
				}
			}
			if (flag) {
				t[j]=temp;
			} else {
				j--;
			}
		}
		for (int j = 0; j < t.length; j++) {
			Judge judge=(Judge)judgequestions.get(t[j]);
			pickedjudge=new PickedJudge();
			pickedjudge.setJudgeId(judge.getId());
			pickedjudge.setTestId(id);
			basicDao.save(pickedjudge);
		}
		return true;
	}
	public String  check_result() {
		HttpServletRequest request = ServletActionContext.getRequest();
		TestDAO testDAO=new TestDAO();
		ResultDAO resultDAO=new ResultDAO();
		Test test=testDAO.getTestById("Test", testId);
		String testName=test.getName();
		double maxScore=resultDAO.getScoreByProperty("max", testId);
		double minScore=resultDAO.getScoreByProperty("min", testId);
		double avgScore=resultDAO.getScoreByProperty("avg", testId);
		List<Object> results=resultDAO.getResultsByTest(testId);
		
		request.setAttribute("testName", testName);
		request.setAttribute("maxScore", maxScore);
		request.setAttribute("minScore", minScore);
		request.setAttribute("avgScore", avgScore);
		request.setAttribute("results", results);
		return SUCCESS;
	}
	
	public String list_test() {
		setTests(basicDao.getAllObjects("Test"));
		for (int i = 0; i < tests.size(); i++) {
			Test test=(Test) tests.get(i);
			Timestamp timestamp=test.getDate();
			String temp="";
			SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
			temp=format2.format(timestamp);
			dates.add(temp);
		}
		return SUCCESS;
	}

	
	
	public void setTests(List<Object> tests) {
		this.tests = tests;
	}

	public List<Object> getTests() {
		return tests;
	}

	public void setDates(List<String> dates) {
		this.dates = dates;
	}

	public List<String> getDates() {
		return dates;
	}
}
